//
//  HTRecentPlayScreen.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/1.
//

import SwiftUI
import PKHUD

/// 历史播放界面
struct HTRecentPlayScreen: View {
    
    @EnvironmentObject private var var_store: HTAppStore
    @State var var_animes: [HTAnime] = []
    @State var var_subId: String? = ""
    @State var var_isEdit: Bool = false
    @State var var_showPop: Bool = false
    @State var var_offset: CGFloat = 110
    @State var var_selIds: [String] = []
    @State private var var_selAnime: HTAnime = HTAnime()
    @State private var var_showPushToVideoDetail: Bool = false
    
    var body: some View {
        HTNavigationBar {
            VStack {
                NavigationLink(destination: HTVideoDetailScreen(var_id: var_selAnime.var_id, var_epId: var_selAnime.var_cur_epId ?? "", var_seekTime: var_selAnime.var_startTime ?? "", var_source: "6")
                               , isActive: $var_showPushToVideoDetail) {
                    EmptyView()
                }
                
                ScrollView{
                    VStack(spacing: 10){
                        ForEach(0..<var_animes.count, id: \.self) { index in
                            let var_anime = var_animes[index]
                            
                            Button {
                                if (!var_isEdit) {
                                    var_selAnime = var_anime
                                    var_showPushToVideoDetail = true
                                } else {
                                    if (var_selIds.contains(var_anime.var_id)) {
                                        var_selIds.remove(at: var_selIds.firstIndex(of: var_anime.var_id)!)
                                    } else {
                                        var_selIds.append(var_anime.var_id)
                                    }
                                }
                            } label: {
                                HTRecentPlayCell(var_anime: var_animes[index], var_isEdit: var_isEdit, var_isSel: var_selIds.contains(var_anime.var_id)) { anime in
                                    if (var_selIds.contains(anime.var_id)) {
                                        var_selIds.remove(at: var_selIds.firstIndex(of: anime.var_id)!)
                                    } else {
                                        var_selIds.append(anime.var_id)
                                    }
                                } var_onMore: { anime in
                                    var_showPop = !var_showPop
                                    if var_showPop {
                                        var_selIds = [anime.var_id]
                                    } else {
                                        var_selIds = []
                                    }
                                }
                            }
                        }
                    }
                    .padding(15)
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(Color(hex: "#f5f5f5"))
                
                if (var_showPop) {
                    VStack{
                        Button {
                            ht_delData()
                        } label: {
                            Text(var_isEdit ? "Delete Selected" : "Delete")
                                .font(.system(size: 14))
                                .foregroundColor(Color(hex: "#FC0B52"))
                                .fontWeight(.medium)
                                .frame(maxWidth: .infinity, maxHeight: 55)
                        }
                        Button {
                            var_showPop = false
                            var_isEdit = false
                            var_selIds = []
                        } label: {
                            Text("Cancel")
                                .font(.system(size: 14))
                                .foregroundColor(Color.black)
                                .fontWeight(.medium)
                                .frame(maxWidth: .infinity, maxHeight: 55)
                        }
                    }
                    .background(Color.white)
                    .frame(height: 110)
                    .clipShape(HTRroundedCorners(var_tl: 30, var_tr: 30))
                    .clipped()
                }
            }
            .toolbar(.hidden,for: .tabBar)
        }
        .ht_title(title: "Recently Played")
        .ht_maxWidth(leading: 80, trailing: 80)
        .ht_navigationBarItems(trailing: {
            Button {
                var_isEdit.toggle()
                if (var_isEdit) {
                    var_showPop = true
                    var_selIds = []
                } else {
                    var_showPop = false
                }
            } label: {
                Text(var_isEdit ? "Select All " : "Delete All")
                    .font(.system(size: 12))
                    .foregroundColor(Color(hex: "#FC0B52"))
                    .fontWeight(.medium)
            }
        })
        .preferredColorScheme(.dark)
        .onReceive(NotificationCenter.default.publisher(for: HTConfig.STATIC_notification_addNewRecentPlay), perform: { obj in
            ht_loadData()
        })
        .onAppear{
            if (var_animes.count == 0) {
                ht_loadData()
            }
        }
    }
    
    func ht_loadData() {
        var_store.dispatch(HTEnumAnimeAction.ht_recentPlayList(callback: { animes in
            self.var_animes = animes ?? []
        }))
    }
    
    func ht_delData() {
        if (var_isEdit && var_selIds.count == 0) {
            HUD.flash(.label("No Selected"), delay: 1.0)
            return
        }
        
        var_store.dispatch(HTEnumAnimeAction.ht_deleteRecentPlay(ids: var_selIds))
        var_selIds = []
        var_showPop = false
        var_isEdit = false
        
        ht_loadData()
    }
}
